//这里定义一些全局通用的函数，该文件会被自动加载
var fs = require('fs');
//遍历获取目录下指定类型文件
global.scanFolder = function (path) {
    var fileList = [],
        folderList = [],
        walk = function (path, fileList, folderList) {
            files = fs.readdirSync(RESOURCE_PATH+"/"+path);
            files.forEach(function (item) {
                var tmpPath = path + '/' + item,
                    stats = fs.statSync(RESOURCE_PATH+"/"+tmpPath);

                if (stats.isDirectory()) {
                    walk(tmpPath, fileList, folderList);
                    folderList.push(tmpPath);
                } else {
                    fileList.push(tmpPath);
                }
            });
        };

    walk(path, fileList, folderList);

    console.log('扫描' + path + '成功');

    return {
        'files': fileList,
        'folders': folderList
    }
}
//判断一个值是否在一个数组中
global.in_array = function (stringToSearch, arrayToSearch) {
    for (var s = 0; s < arrayToSearch.length; s++) {
        var thisEntry = arrayToSearch[s].toString();
        if (thisEntry == stringToSearch) {
            return true;
        }
    }
    return false;
}


/**
 * 排序函数
 */
function sort_node(v, w) {
    return v["taxis"] - w["taxis"];
}

/**
 * 获取子集分类 （这里是获取所有子集）
 */
global.get_children = function (nodes, parent) {
    var children = [];
    var last = [];
    /* 未访问的节点 */
    /*
     * 获取根分类列表。
     * 创建一个虚拟父级分类亦可
     **/
    var node = null;
    for (var i in nodes) {
        node = nodes[i];
        if (node["pid"] == parent) {
            node["deep"] = 0;
            children.push(node);
        } else {
            last.push(node);
        }
    }
    children.sort(sort_node);

    /* 同级排序 */
    var jumper = 0;
    var stack = children.slice(0);
    /* easy clone */


    while (stack.length > 0
        /* just in case */ && jumper++ < 200) {
        var shift_node = stack.shift();
        var list = [];
        /* 当前子节点列表 */

        var last_static = last.slice(0);
        last = [];
        for (var i in last_static) {
            node = last_static[i];
            if (node["pid"] == shift_node["cid"]) {
                node["deep"] = shift_node["deep"] + 1;
                list.push(node);
            } else {
                last.push(node);
            }
        }
        list.sort(sort_node);

        for (var i in list) {
            node = list[i];
            stack.push(node);
            children.push(node);
        }
    }
    /*
     * 有序树非递归前序遍历
     * http://ds.fzu.edu.cn/fine/resources/FlashContent.asp?id=51
     * */
    var stack = [];
    /* 前序操作栈 - 分类编号 */
    var top = null;
    /* 操作栈顶 */
    var tree = children.slice(0);
    /* 未在前序操作栈内弹出的节点 */
    var has_child = false;
    /* 是否有子节点，如无子节点则弹出栈顶 */
    var children = [];
    /* 清空结果集 */
    var jumper = 0;
    last = [];
    /* 未遍历的节点 */
    var current = null;
    /* 当前节点 */
    stack.push(parent);
    /* 建立根节点 */

    while (stack.length > 0) {
        if (jumper++ > 200) {
            break;
        }
        top = stack[stack.length - 1];
        has_child = false;
        last = [];

        for (var i in tree) {
            current = tree[i];
            if (current["pid"] == top) {
                top = current["cid"];
                stack.push(top);
                children.push(current);
                has_child = true;
            } else {
                last.push(current);
            }
        }
        tree = last.slice(0);
        if (!has_child) {
            stack.pop();
            top = stack[stack.length - 1];
        }
    }
    return children;
}
global.times = function (d) {
    //日期格式化
    var date = new Date(d);
    var y = date.getFullYear();
    var M = date.getMonth() + 1;
    M = M < 10 ? "0" + M : M;
    var d = date.getDate();
    d = d < 10 ? "0" + d : d;
    var h = date.getHours();
    h = h < 10 ? "0" + h : h;
    var m = date.getMinutes();
    m = m < 10 ? "0" + m : m;
    var time = y + "-" + M + "-" + d + " " + h + ":" + m;
    return time;
}

global.getFullName = function (conf, oriName, type) {
    //type： 1没有文件后缀
    //替换目录日期事件
    ///ueditor/php/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}
    var self = this;
    //var filename= this.filed;
    var format = conf.pathFormat;
    var d = new Date();
    var t = d.getTime();
    var date = {};
    date.yyyy = d.getFullYear();
    date.mm = (d.getMonth() + 1) < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1;
    date.dd = (d.getDate() < 10 ? "0" : "") + d.getDate();
    format = format.replace("{yyyy}", date.yyyy);
    format = format.replace("{mm}", date.mm);
    format = format.replace("{dd}", date.dd);
    format = format.replace("{time}", t);
    //计算随机数
    var str = format.match(/\{rand\:[\d]*\}/i)
    var index = str[0].search(/:/) + 1;
    index = str[0].charAt(index);
    var rand = Math.random();
    rand = rand.toString();
    rand = rand.substr(2, index);
    //替换随机数
    format = format.replace(str[0], rand);
    var ext = self.getFileExt(oriName);
    if (type == 1) {
        return format;
    } else {
        return format + ext;
    }

}
//获取文件扩展名
global.getFileExt = function (oriName) {
    var filename = oriName;
    return filename.substr(filename.lastIndexOf(".")).toLocaleLowerCase();
}
//获取文件名称
global.getFileName = function (fullname) {
    return fullname.substr(fullname.lastIndexOf("/") + 1);
}
//创建目录
global.creatdir = function (re) {
//re 文件路径
//    console.log(RESOURCE_PATH);
//    if (!isDir("eee")) {
//        fs.mkdirSync("eee");
//    }
//    fs.writeFile("aa.txt", "dfdfs")
//    console.log(re)
    var deferred = getDefer();
    var n = re.lastIndexOf("/");
    re = re.substr(0, n);
    re = re.split("/");
    var b = [];
    for (var i = 0; i < re.length; i++) {
        var a = re[i];
        b[i] = a;
        var ss = b.join("/");
        if (!isDir(RESOURCE_PATH+"/"+ss)) {
            fs.mkdirSync(RESOURCE_PATH+"/"+ss);
            console.log(a + '目录创建成功');
            console.log(RESOURCE_PATH+"/"+ss);
        }

    }
    deferred.resolve(true);
    return deferred.promise;
}
//保存文件
global.savefile = function (filpath, fullname) {
    var deferred = getDefer();
    fs.readFile(filpath, function (err, data) {
        if (err) {
            console.log("222222");
        } else {
            //写入文件到uplaod
            fs.writeFile(RESOURCE_PATH+"/"+fullname, data, function (err) {
                if (err) {
                    console.log('There was an error when write file');
                } else {
                    //console.log('保存成功');
                    //console.log(data);
                    //console.log(fullname);
                    deferred.resolve(fullname);

                }
            });
        }
    });

    return deferred.promise;
}
global.wall=function(){
    var GetRandomNum=function (Min,Max)
    {
        var Range = Max - Min;
        var Rand = Math.random();
        return(Min + Math.round(Rand * Range));
    }
    var arr = [4,5,7,7,7,10,11,12];
    var get = arr[GetRandomNum(0,7)];
    var image = '/resource/images/wall_page/'+get+".jpg";
    return image;
}